package com.luguokong.pet_rescue.controller;

import com.alibaba.fastjson.JSON;
import com.luguokong.pet_rescue.pojo.User;
import com.luguokong.pet_rescue.service.UserService;
import com.luguokong.pet_rescue.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserCtrl {

    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public Map<String,Object> login(String phone, String password){
        Map<String,Object> map = new HashMap<>();
        int flag = userService.login(phone, password);
        if (flag == 1) {
            // 生成JWT令牌
            Map<String,String> payloadMap = new HashMap<>();
            payloadMap.put("userType","管理员");
            payloadMap.put("phone",phone);
            String token = JwtUtil.createToken(payloadMap);
            // 返回验证信息
            map.put("state", true);
            map.put("msg","认证成功");
            map.put("token",token);
        } else {
            map.put("state", false);
            map.put("msg","认证失败");
        }
        return map;
    }

    @PostMapping("/register")
    public int registerUser(@RequestBody User user) {
//        String user_type, String name, String phone, int age, String password1, String password2
//        User user = new User();
        int flag = 0;
//        if (password1.equals(password2)){
//            user.setUserType(user_type);
//            user.setName(name);
//            user.setPhone(phone);
//            user.setAge(age);
//            user.setPassword(password1);
//            flag = userService.registerUser(user);
//        }
        flag = userService.registerUser(user);
        return flag;
    }

    @GetMapping("/getUserById")
    public String selectUserById(Integer user_id){
        User user = userService.selectUserById(user_id);
        return JSON.toJSONString(user);
    }

    @GetMapping("/getAllUser")
    public List<User> selectUserAll(){
        List<User> userList = userService.selectUserAll();
        return userList;
    }

    @GetMapping("/getUserByPhone")
    public String selectUserByPhone(String phone){
        User user = userService.selectUserByPhone(phone);
        return JSON.toJSONString(user);
    }

    @GetMapping("/getByPage")
    public List<User> selectUserAllByPageNumByPageSize(int pageNum, int pageSize){
        List<User> userList = userService.selectUserAllByPageNumByPageSize((pageNum-1)*pageSize, pageSize);
        return userList;
    }

    @GetMapping("/getDataCount")
    public int selectDataCount(){
        int i = userService.selectDataCount();
        return i;
    }

    @PostMapping("/insertUser")
    public int insertUser(@RequestBody User user){
        int i = userService.insertUser(user);
        return i;
    }

    @GetMapping("/deleteUserById")
    public int forceDeleteUserById(int userId){
        int i = userService.forceDeleteUserById(userId);
        return i;
    }

    @PostMapping("/updateUserById")
    public int updateUserById(@RequestBody User user){
        int i = userService.updateUserById(user);
        return i;
    }

}